package cn.blog.dao.front;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import cn.blog.dao.conn.DataManager;
import cn.blog.dao.conn.ParamSet;
import cn.blog.dao.conn.RowDatas;
import cn.blog.entity.addBean.BlogArtInfo;
import cn.blog.entity.origin.Article;
import cn.blog.entity.origin.Article2;

public class ArtilceDao extends DataManager {

	private RowDatas<Article> artilce;// 文章对象
	private RowDatas<Integer> oneData;// 文章对象
	private RowDatas<String> stringData;

	/**
	 * 无参构造 初始化
	 */
	public ArtilceDao() {
		artilce = new articleData();
		oneData = new oneData();
		stringData = new oneStringData();
	}

	/**
	 * 查询文章总数
	 * 
	 * @return
	 */
	public int artCount() {
		String sql = "SELECT COUNT(*) FROM tb_article where art_state ='已发表'";
		return query(sql, oneData);

	}

	/**
	 * 按照时间先后文章类型查询已发表文章 查询4条
	 * 
	 * @param index
	 *            索引 既当前页
	 * @param currentCount
	 *            每页条数
	 * @return 文章集合
	 */
	public List<Article> artAll(int claId1, int index, int currentCount) {
		
		String sql = "SELECT * FROM tb_article WHERE art_state ='已发表' AND  art_class_id = ? 	ORDER BY art_time DESC LIMIT ?,?";

		return queryList(sql, new ParamSet(claId1, index, currentCount), artilce);
	}

	/**
	 * 按照时间先后查询已发表文章 查询4条
	 * 
	 * @param index
	 *            索引 既当前页
	 * @param currentCount
	 *            每页条数
	 * @return 文章集合
	 */
	public List<Article> artAll(int index, int currentCount) {
		String sql = "SELECT * FROM tb_article WHERE art_state ='已发表' 	ORDER BY art_time DESC LIMIT ?,?";
		return queryList(sql, new ParamSet(index, currentCount), artilce);
	}

	/**
	 * 按照时间先后查询已发表文章
	 * 
	 * @return 文章集合
	 */
	public List<Article> artAll() {
		String sql = "SELECT * FROM tb_article WHERE art_state ='已发表' ORDER BY art_time DESC ";
		return queryList(sql, artilce);
	}

	/**
	 * 根据文章编号查询文章信息
	 * 
	 * @param artId
	 *            章编号
	 * @return 文章对象
	 */
	public Article art(int artId) {
		String sql = "SELECT * FROM tb_article WHERE art_id = ?";
		return query(sql, new ParamSet(artId), artilce);
	}

	/**
	 * 根据用户编号查找所有文章
	 * 
	 * @param userId
	 *            用户编号
	 * @return 所有文章集合
	 */
	public List<Article> userArt(int userId) {
		String sql = "SELECT * FROM tb_article WHERE art_user_id = ? and art_state ='已发表'";
		return queryList(sql, new ParamSet(userId), artilce);
	}

	/**
	 * 一周内的阅读排行
	 * 
	 * @param begTime
	 *            开始时间
	 * @param endTime
	 *            结束时间
	 * @return 文章集合
	 */
	public List<Article> weekArt(Date begTime, Date endTime) {
		String sql = "SELECT * FROM tb_article WHERE  art_state ='已发表' and art_time BETWEEN ? AND ? ORDER BY art_read DESC LIMIT 0,10 ";
		return queryList(sql, new ParamSet(begTime, endTime), artilce);

	}

	/**
	 * 获取数据源
	 * 
	 * @author 江明
	 * @time 2018年3月17日
	 * @function
	 */
	private class articleData implements RowDatas<Article> {

		@Override
		public Article getRowData(ResultSet rs) throws SQLException {
			Article art = new Article();
			art.setArtId(rs.getInt(1));
			art.setArtUserId(rs.getInt(2));
			art.setArtTitle(rs.getString(3));
			art.setArtClassId(rs.getInt(4));
			art.setArtTime(rs.getDate(5));
			art.setArtRead(rs.getInt(6));
			art.setArtContext(rs.getString(7));
			art.setArtState(rs.getString(8));
			return art;
		}

	}

	private class oneData implements RowDatas<Integer> {

		@Override
		public Integer getRowData(ResultSet rs) throws SQLException {
			// TODO Auto-generated method stub
			return rs.getInt(1);
		}

	}

	private class oneStringData implements RowDatas<String> {

		@Override
		public String getRowData(ResultSet rs) throws SQLException {
			// TODO Auto-generated method stub
			return rs.getString(1);
		}

	}

	public int getClaId(String claName) {
		String sql = "SELECT class_id FROM tb_article_class WHERE class_name = ?";
		return query(sql, new ParamSet(claName), oneData) == null ? 0 : query(sql, new ParamSet(claName), oneData);
	}

	/**
	 * 根据文章名按时间条件查询文章
	 * 
	 * @param artName
	 *            文章名
	 * @param index
	 *            起始数
	 * @param currentCount
	 *            文章条数
	 * @return
	 */
	public List<Article> artAll(String artName, int index, int currentCount) {
		String sql = "SELECT * FROM tb_article WHERE art_state ='已发表' AND  art_title LIKE '%" + artName
				+ "%' 	ORDER BY art_time DESC LIMIT ?,? ";
		return queryList(sql, new ParamSet(index, currentCount), artilce);
	}

	public int addTitle(int uid, String title, int claId, Date artTime, int read, String artText, String state) {
		String sql = "insert into tb_article (art_user_id ,art_title ,art_class_id ,art_time ,art_read ,art_context ,art_state )"
				+ "values(?,?,?,?,?,?,?)";
		return updateExecuted(sql, new ParamSet(uid, title, claId, artTime, read, artText, state));
	}

	public int updateState(int parseInt) {
		String sql = "UPDATE tb_article SET art_state ='作废' WHERE art_id = ?";
		return updateExecuted(sql, new ParamSet(parseInt));
	}

	public int updateArt(String xgartTitle, int parseInt, int parseInt2, String string) {
		String sql = "UPDATE tb_article SET art_title =?,art_class_id =? WHERE art_id = ? and art_state = ?";
		return updateExecuted(sql, new ParamSet(xgartTitle,parseInt,parseInt2,string));
	}

	public int updateState(String state, int parseInt) {
		String sql = "UPDATE tb_article SET art_state =? WHERE art_id = ?";
		return updateExecuted(sql, new ParamSet(state,parseInt));
	}

	public int updateRead(int read, int artId) {
		String sql = "UPDATE tb_article SET art_read =? WHERE art_id = ?";
		return updateExecuted(sql, new ParamSet(read,artId));
	}

	
}
